package org.jeecg.modules.hc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.hc.entity.HcSystemConf;
import org.jeecg.modules.hc.mapper.HcSystemConfMapper;
import org.jeecg.modules.hc.service.IHcSystemConfService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: 系统配置
 * @Author: jeecg-boot
 * @Date:   2022-12-13
 * @Version: V1.0
 */
@Service
public class HcSystemConfServiceImpl extends ServiceImpl<HcSystemConfMapper, HcSystemConf> implements IHcSystemConfService {

    @Override
    public Map<String, String> getAllSystemConf() {
        Map<String, String> result = new HashMap<>();
        List<HcSystemConf> list = this.list();

        if (!CollectionUtils.isEmpty(list)) {
            result = list.stream().collect(Collectors.toMap(HcSystemConf::getConfKey, HcSystemConf::getConfVal));
        }

        return result;
    }

    @Override
    public void submit(Map<String, String> param) {
        for (String key : param.keySet()) {
            String value = param.get(key);

            if (StringUtils.isNotEmpty(value)) {
                HcSystemConf hcSystemConf = new HcSystemConf();
                hcSystemConf.setConfKey(key);
                hcSystemConf.setConfVal(value);

                QueryWrapper<HcSystemConf> updateWrapper = new QueryWrapper<>();
                updateWrapper.eq("conf_key", hcSystemConf.getConfKey());
                this.saveOrUpdate(hcSystemConf, updateWrapper);
            }
        }
    }
}
